<template>
  <div class="home-container">
    <!-- 顶部导航栏 -->
    <header class="home-header">
      <div class="header-left">
        <h1 class="system-title">IM 聊天系统后台管理</h1>
      </div>
      <div class="header-right">
        <div class="user-info">
          <span class="username">欢迎，{{ username }}</span>
          <el-button type="text" @click="handleLogout">退出登录</el-button>
        </div>
      </div>
    </header>

    <!-- 主要内容区域 -->
    <main class="home-main">
      <!-- 统计卡片区域 -->
      <section class="statistics-section">
        <h2 class="section-title">系统概览</h2>
        <div class="stats-cards">
          <el-card class="stat-card" :hoverable="true" @mouseenter="handleCardHover(0)" @mouseleave="handleCardLeave(0)">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="40"><User /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ stats.userCount }}</div>
                <div class="stat-label">用户总数</div>
              </div>
            </div>
          </el-card>
          
          <el-card class="stat-card" :hoverable="true" @mouseenter="handleCardHover(1)" @mouseleave="handleCardLeave(1)">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="40"><ChatSquare /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ stats.roomCount }}</div>
                <div class="stat-label">房间/队伍数</div>
              </div>
            </div>
          </el-card>
          
          <el-card class="stat-card" :hoverable="true" @mouseenter="handleCardHover(2)" @mouseleave="handleCardLeave(2)">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="40"><Star /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ stats.aiTokenCount }}</div>
                <div class="stat-label">AI Token 剩余数</div>
              </div>
            </div>
          </el-card>
        </div>
      </section>

      <!-- 系统通知区域 -->
      <section class="notifications-section">
        <div class="section-header">
          <h2 class="section-title">系统通知</h2>
          <el-button type="text" @click="markAllAsRead" v-if="hasUnreadNotifications">
            全部标记为已读
          </el-button>
        </div>
        <el-card class="notifications-card">
          <el-empty v-if="notifications.length === 0" description="暂无通知消息" />
          <div v-else class="notifications-list">
            <div 
              v-for="notification in notifications" 
              :key="notification.id"
              class="notification-item"
              :class="{ 'unread': !notification.read }"
              @click="markAsRead(notification.id)"
            >
              <div class="notification-header">
                <div class="notification-title">{{ notification.title }}</div>
                <div class="notification-time">{{ formatTime(notification.time) }}</div>
              </div>
              <div class="notification-content">{{ notification.content }}</div>
            </div>
          </div>
        </el-card>
      </section>

      <!-- 进入后台管理按钮 -->
      <section class="action-section">
        <el-button 
          type="primary" 
          size="large" 
          @click="goToAdminDashboard"
          class="admin-button"
        >
          <el-icon><Setting /></el-icon>
          进入后台管理
        </el-button>
      </section>
    </main>

    <!-- 页脚 -->
    <footer class="home-footer">
      <p>IM 聊天系统后台管理 © 2025</p>
    </footer>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, ChatSquare, Star, Setting } from '@element-plus/icons-vue'

const router = useRouter()
const username = ref('')
const activeCard = ref(-1)

// 统计数据
const stats = reactive({
  userCount: 0,
  roomCount: 0,
  aiTokenCount: 0
})

// 通知数据
const notifications = ref([])

// 检查是否有未读通知
const hasUnreadNotifications = computed(() => {
  return notifications.value.some(notification => !notification.read)
})

// 格式化时间
const formatTime = (time) => {
  const date = new Date(time)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 处理卡片悬停
const handleCardHover = (index) => {
  activeCard.value = index
}

const handleCardLeave = () => {
  activeCard.value = -1
}

// 标记单个通知为已读
const markAsRead = (id) => {
  const notification = notifications.value.find(n => n.id === id)
  if (notification && !notification.read) {
    notification.read = true
    ElMessage.success('已标记为已读')
  }
}

// 标记所有通知为已读
const markAllAsRead = () => {
  notifications.value.forEach(notification => {
    notification.read = true
  })
  ElMessage.success('已全部标记为已读')
}

// 跳转到后台管理
const goToAdminDashboard = () => {
  router.push('/admin/users')
}

// 退出登录
const handleLogout = () => {
  sessionStorage.removeItem('isLoggedIn')
  sessionStorage.removeItem('username')
  ElMessage.success('已退出登录')
  router.push('/login')
}

// 模拟加载数据
const loadMockData = () => {
  // 模拟统计数据
  stats.userCount = 1280
  stats.roomCount = 256
  stats.aiTokenCount = 500000
  
  // 模拟通知数据
  notifications.value = [
    {
      id: 1,
      title: '系统维护通知',
      content: '系统将于本周日凌晨2:00-4:00进行例行维护，请提前做好准备。',
      time: new Date(Date.now() - 3600000).toISOString(),
      read: false
    },
    {
      id: 2,
      title: '新功能上线',
      content: 'AI 智能回复功能已正式上线，管理员可在后台配置相关参数。',
      time: new Date(Date.now() - 86400000).toISOString(),
      read: false
    },
    {
      id: 3,
      title: '安全提醒',
      content: '近期发现多起钓鱼攻击，请提醒用户注意账号安全，不要点击可疑链接。',
      time: new Date(Date.now() - 172800000).toISOString(),
      read: true
    }
  ]
}

// 页面加载时获取数据
onMounted(() => {
  // 获取用户名
  const storedUsername = sessionStorage.getItem('username')
  if (storedUsername) {
    username.value = storedUsername
  }
  
  // 加载模拟数据
  loadMockData()
  
  // 添加淡入动画
  document.querySelector('.home-main').classList.add('fade-in')
})
</script>

<style scoped>
.home-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
  overflow: hidden;
}

/* 顶部导航栏 */
.home-header {
  background: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 0 24px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
  z-index: 100;
}

.system-title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 16px;
}

.username {
  color: #606266;
}

/* 主要内容区域 */
.home-main {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  opacity: 0;
  transform: translateY(20px);
  transition: opacity 0.5s ease, transform 0.5s ease;
}

.fade-in {
  opacity: 1;
  transform: translateY(0);
}

/* 区块标题 */
.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 16px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

/* 统计卡片区域 */
.statistics-section {
  margin-bottom: 24px;
}

.stats-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.stat-card {
  transition: all 0.3s ease;
  cursor: pointer;
  border: 2px solid transparent;
}

.stat-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
}

.stat-content {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px 0;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.stat-info {
  flex: 1;
}

.stat-number {
  font-size: 28px;
  font-weight: 700;
  color: #303133;
  line-height: 1;
}

.stat-label {
  font-size: 14px;
  color: #606266;
  margin-top: 8px;
}

/* 通知区域 */
.notifications-section {
  margin-bottom: 24px;
}

.notifications-card {
  background: white;
}

.notifications-list {
  max-height: 400px;
  overflow-y: auto;
}

.notification-item {
  padding: 16px;
  border-bottom: 1px solid #ebeef5;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.notification-item:hover {
  background-color: #f5f7fa;
}

.notification-item.unread {
  background-color: #f0f9ff;
}

.notification-item.unread::before {
  content: '';
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #409eff;
  margin-right: 8px;
  vertical-align: middle;
}

.notification-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.notification-title {
  font-weight: 600;
  color: #303133;
}

.notification-time {
  font-size: 12px;
  color: #909399;
}

.notification-content {
  font-size: 14px;
  color: #606266;
  line-height: 1.5;
}

/* 操作按钮区域 */
.action-section {
  display: flex;
  justify-content: center;
  margin-top: 32px;
}

.admin-button {
  padding: 12px 32px;
  font-size: 16px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
}

.admin-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(102, 126, 234, 0.4);
}

/* 页脚 */
.home-footer {
  text-align: center;
  padding: 20px;
  color: #909399;
  font-size: 14px;
  background: white;
  border-top: 1px solid #ebeef5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .home-main {
    padding: 16px;
  }
  
  .stats-cards {
    grid-template-columns: 1fr;
  }
  
  .system-title {
    font-size: 18px;
  }
}
</style>